package com.peng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.exception.YyghException;
import com.peng.result.Result;
import com.peng.service.HospitalSetService;
import com.peng.utils.MD5;
import com.peng.yygh.model.hosp.HospitalSet;
import com.peng.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Api(tags = "医院设置管理")
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("/{id}")
    public Result<Boolean> removeHospSet(@PathVariable("id") Long id){
        boolean b = hospitalSetService.removeById(id);
        if (b)
            return Result.ok(true);
        else
            return Result.fail(false);
    }
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findPageHospSet(@PathVariable("current") Integer current,
                                                     @PathVariable("limit")Integer limit,
                                                     @RequestBody(required = false) HospitalSetQueryVo hospitalQueryVo){
        Page<HospitalSet> hospitalSetPage = new Page<>(current, limit);
        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(hospitalQueryVo.getHosname()!=null,HospitalSet::getHosname,hospitalQueryVo.getHosname());
        queryWrapper.like(hospitalQueryVo.getHoscode()!=null,HospitalSet::getHoscode,hospitalQueryVo.getHoscode());
        Page<HospitalSet> page = hospitalSetService.page(hospitalSetPage, queryWrapper);
        return Result.ok(page);
    }
    //4 添加医院设置
    @PostMapping("saveHospitalSet")
    public Result<Boolean> saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        hospitalSetService.save(hospitalSet);
        return Result.ok();
    }

    @GetMapping("/getHospSet/{id}")
    public Result<HospitalSet> getHospSet(@PathVariable("id")Long id){
        try {
            int i=1/0;
        } catch (Exception e) {
            throw new YyghException("失败",202);
        }
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }


    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> list){
        hospitalSetService.removeByIds(list);
        return Result.ok();
    }

    //8 医院设置锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet byId = hospitalSetService.getById(id);
        byId.setStatus(status);
        hospitalSetService.updateById(byId);
        return Result.ok();
    }

    //9 发送签名秘钥
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        //发送短信

        return Result.ok();
    }
}
